package com.xms.security.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.organization.entity.Systemuser;
import com.xms.organization.service.ISystemUserService;
import com.xms.security.api.model.SavePrincipalObjectAccessModel;
import com.xms.security.entity.PrincipalObjectAccess;
import com.xms.security.service.dataauth.IPrincipalObjectAccessService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-09-11
 */
@RestController
@RequestMapping("/{org}/api/security/principalobjectaccess")
public class PrincipalObjectAccessController extends ApiBaseController {
    private IPrincipalObjectAccessService _principalObjectAccessService;
    private ISystemUserService _systemUserService;
    @Autowired
    public PrincipalObjectAccessController(IPrincipalObjectAccessService principalObjectAccessService,
                                           ISystemUserService systemUserService){
        _principalObjectAccessService = principalObjectAccessService;
        _systemUserService = systemUserService;
    }

    @ApiOperation("获取数据授权信息")
    @GetMapping("getbyobject")
    public JsonResultObject getByObject(@RequestParam(value="entityid", required = true) UUID entityid,
                                        @RequestParam(value="objectid", required = true) UUID objectid){
        if(UUIDUtil.isEmpty(entityid) || UUIDUtil.isEmpty(objectid)){
            return JNotSpecifiedParams("entityid", "objectid");
        }
        List<PrincipalObjectAccess> result = _principalObjectAccessService.list(new LambdaQueryWrapper<PrincipalObjectAccess>()
                .eq(PrincipalObjectAccess::getEntityid, entityid).eq(PrincipalObjectAccess::getObjectid, objectid));
        if(CollectionUtil.notEmpty(result)){
            List<Systemuser> users = _systemUserService.list(new LambdaQueryWrapper<Systemuser>()
                    .in(Systemuser::getSystemuserid, result.stream().map(f->f.getPrincipalid()).collect(Collectors.toList())));
            if(CollectionUtil.notEmpty(users)) {
                result.forEach(x -> {
                    Systemuser user = users.stream().filter(u->u.getSystemuserid().equals(x.getPrincipalid())).findFirst().orElse(null);
                    if(user != null)
                        x.setPrincipalidname(user.getName());
                });
            }
        }
        return JOk(result);
    }

    @ApiOperation("保存数据授权信息")
    @PostMapping("savebyobject")
    public JsonResultObject saveByObject(@RequestBody SavePrincipalObjectAccessModel model){
        if(UUIDUtil.isEmpty(model.entityId) || UUIDUtil.isEmpty(model.objectId)){
            return JNotSpecifiedParams("entityid", "objectid");
        }
        
        return JSaveSuccess();
    }
}
